<?php
// 启动会话
session_start();

// 包含数据库连接代码
include 'conn.php';
include 'header.php';

// 检查用户是否已登录
if (!isset($_SESSION['user_id'])) {
    // 用户未登录，重定向到登录页面
    echo '<script>alert("请先登录"); window.location.href = "login.html";</script>';
    exit();
}

// 获取游戏ID
$game_id = $_GET['game_id'];

// 获取游戏详细信息的函数
function getGameDetails($conn, $game_id)
{
    $sql = "SELECT * FROM games WHERE game_id = $game_id";
    $result = executeQuery($conn, $sql);

    if ($result && mysqli_num_rows($result) > 0) {
        $row = mysqli_fetch_assoc($result);

        $title = $row['title'];
        $description = $row['description'];
        $release_date = $row['release_date'];
        $publisher = $row['publisher'];
        $rating = $row['rating'];
        $image_path = $row['image_path'];

        // 释放结果集
        mysqli_free_result($result);

        return [
            'title' => $title,
            'description' => $description,
            'release_date' => $release_date,
            'publisher' => $publisher,
            'rating' => $rating,
            'image_path' => $image_path,
        ];
    } else {
        // 处理未找到游戏的情况
        return null;
    }
}

// 获取游戏详细信息
$gameDetails = getGameDetails($conn, $game_id);

// 如果未找到游戏信息，进行相应处理
if ($gameDetails === null) {
    echo '未找到游戏信息';
    exit();
}
?>
<?php
function executeQuery($conn, $sql)
{
    $result = mysqli_query($conn, $sql);
    return $result;
} 

?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>游戏详情</title>
    <!-- Bootstrap CSS 链接 -->
    <link rel="stylesheet" href="./common/css/bootstrap.min.css">
    <link rel="stylesheet" href="./common/css/detail.css">
</head>

<body>

    <div class="container mt-4">
        <div class="card">
            <img src="<?php echo $gameDetails['image_path']; ?>" class="card-img-top" alt="<?php echo $gameDetails['title']; ?>">
            <div class="card-body">
                <h5 class="card-title"><?php echo $gameDetails['title']; ?></h5>
                <p class="card-text description"><?php echo $gameDetails['description']; ?></p>
                <p class="card-text"><small class="text-muted">发行日期: <?php echo $gameDetails['release_date']; ?></small></p>
                <p class="card-text"><small class="text-muted">出版公司: <?php echo $gameDetails['publisher']; ?></small></p>
                <p class="card-text"><small class="text-muted">评分: <?php echo $gameDetails['rating']; ?></small></p>

                <!-- 按钮组 -->
                <div class="btn-group" role="group">
                    </button> <a href="index.php" class="btn btn-sm btn-secondary">返回首页</a>
                </div>
            </div>
        </div>
    </div>

    <script src="./common/js/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/@popperjs/core@2.9.2/dist/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>

</html>